import time
import logging
import RPi.GPIO as GPIO


logging.basicConfig(level=logging.DEBUG)
LOG = logging.getLogger()


class SetPin:
    def __init__(self, port):
        self._port = port
        GPIO.setup(self._port, GPIO.OUT, initial=GPIO.LOW)

    def change_voltage(self, _input=None):
        _input = True if _input else False
        print('{} port is {}'.format(self._port, _input))
        GPIO.output(self._port, _input)


class SetPwm:
    def __init__(self, port, freq, dc=0.0001):
        self._port, self._freq, self._dc = port, freq, dc
        GPIO.setup(self._port, GPIO.OUT, initial=GPIO.LOW)
        GPIO.output(self._port, True)
        self._pwm = GPIO.PWM(self._port, self._freq)
        self._pwm.start(self._dc)

    def change_dc(self, dc):
        self._dc = dc
        LOG.info('change {} port dc={}'.format(self._port, self._dc))
        self._pwm.ChangeFrequency(self._freq)
        self._pwm.ChangeDutyCycle(self._dc)

    def change_freq(self, freq):
        self._freq = freq
        LOG.info('change {} port freq={}'.format(self._port, self._freq))
        self._pwm.ChangeFrequency(self._freq)
        self._pwm.ChangeDutyCycle(self._dc)

    def stop(self):
        self._pwm.stop()
